package ke07.pm.reihe1;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

import ke07.pm.template.HundredItemBag;

public class HundredItemClass<T> implements HundredItemBag<T> {

	private List<T> al = new ArrayList<T>();

	@Override
	public int size() {
		return al.size();
	}

	@Override
	public boolean add(T obj) {
		if (obj == null)
			throw new NullPointerException();

		if (al.contains(obj)) {
			al.add(obj);
			return true;
		} else {
			al.add(obj);
			return false;
		}
	}

	@Override
	public boolean remove(T obj) {
		if(obj == null) throw new IllegalArgumentException();
		if (this.contains(obj)) {
			al.remove(obj);
			if (count(obj) == 0){
				return true;
			}
		}
		return false;
		
		
	}

	@Override
	public int count(T obj) {
		int count = 0;
		ListIterator iterator = al.listIterator();
		while (iterator.hasNext()) {
			if (iterator.next().equals(obj))
				count++;
		}
		return count;
	}

	@Override
	public boolean contains(T obj) {
		if (al.contains(obj))
			return true;
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public T[] toArray() {

		return (T[]) al.toArray();
	}

}
